package com.smartrm.trade.application;

import com.smartrm.globalinfrcore.annoation.AppService;
import com.smartrm.globalinfrcore.cmd.AbstractCommandContext;
import com.smartrm.globalinfrcore.cmd.CommandExecutor;
import com.smartrm.globalinfrcore.exception.DomainException;
import com.smartrm.trade.application.dto.SelectCommodityCmdDto;
import com.smartrm.trade.application.dto.VendingMachineCommodityListDto;
import com.smartrm.trade.application.executor.SelectCommodityCmd;
import com.smartrm.trade.application.executor.ShowCommodityListCmd;
import com.smartrm.trade.application.executor.VendingCommandContext;
import com.smartrm.trade.domain.SlotVendingMachineDO;
import com.smartrm.trade.domain.remote.TradeCommodityService;
import com.smartrm.trade.domain.repository.VendingMachineRepository;
import com.smartrm.trade.domain.valueobject.PaymentQrCodeVOJ;
import com.smartrm.trade.domain.valueobject.VendingMachineCommodityListVOJ;
import com.smartrm.trade.infrasturcture.TradeError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/*
 * @program: common-starters
 * @description: 应用服务
 * @author: houhong
 * @create: 2023-01-10 17:01
 */
@AppService
public class AppTradeService {


    private static final Logger logger = LoggerFactory.getLogger(AppTradeService.class);


    @Autowired
    @Qualifier(value = "tradeVendingMachineRepository")
    VendingMachineRepository machineRepository;

    @Autowired
    @Qualifier(value = "antiCorrosionTradeCommodityServiceImpl")
    TradeCommodityService commodityService;

    @Autowired
    private CommandExecutor commandExecutor;

    @Autowired
    private SelectCommodityCmd selectCommodityCmd;

    @Autowired
    private ShowCommodityListCmd showCommodityListCmd;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public PaymentQrCodeVOJ selectCommodity(SelectCommodityCmdDto dto) throws Exception {



        //使用资源库获取 货道售卖机资源库
        SlotVendingMachineDO slotVendingMachineDO = machineRepository.getSlotVendingMachineById(dto.getMachineId());

        if (slotVendingMachineDO == null) {
            logger.info("not find vendinfMatchine:matcheid= {}", dto.getMachineId());
            throw new DomainException(TradeError.VendingMachineNotFound);
        }
        //// TODO: 2023/1/11:使用使用命令模式,执行事件风暴分析法的中的请求命令
        VendingCommandContext vendingCommodityContext = new VendingCommandContext();
        BeanUtils.copyProperties(dto,vendingCommodityContext);
        commandExecutor.setCommandContext(vendingCommodityContext);
        return commandExecutor.execute(selectCommodityCmd);
    }

    /*
     * 顾客查看当前活货道售卖机商品列表
     */
    public VendingMachineCommodityListDto vendingMachineShowCommodityList(Long machineId) throws Exception {

        logger.info("【正在获取当前货道售卖机:{},上面的商品列表】", machineId);
        SlotVendingMachineDO slotVendingMachineDO = machineRepository.getSlotVendingMachineById(machineId);

        VendingCommandContext vendingCommodityContext = new VendingCommandContext();
        vendingCommodityContext.setMachineId(machineId);
        commandExecutor.setCommandContext(vendingCommodityContext);
        VendingMachineCommodityListVOJ voj = commandExecutor.execute(showCommodityListCmd);
        return new VendingMachineCommodityListDto(voj, slotVendingMachineDO.getState());
    }
}